<template>
  <!-- 选择订单 -->
  <view class="order-list">
    <view v-for="(order, index) in orders" :key="index" class="order-card">
      <view class="store-header">
        <view class="store-header-tit">{{ order.shopName }}</view>
        <view class="store-header-right">
          <!-- 订单状态 -->
          <view class="status">{{ order.statusName }}</view>
          <!-- 入库状态 -->
          <view class="status1">{{ order.storageStatusName }}</view>
        </view>
      </view>

      <view class="products">
        <view v-for="(product, pIndex) in order.productList" :key="pIndex" class="product-item">
          <view class="product-left">
            <view class="product-img">
              <u-image :src="product.image" width="160rpx" height="160rpx" radius="8"></u-image>
            </view>
            <view class="product-info">
              <view class="name">{{ product.name }}</view>
              <view class="price">¥{{ product.price.toFixed(2) }}</view>
            </view>
          </view>
          <view>
            <view class="product-right">
              <view class="num">共{{ product.quantity }}件</view>
            </view>
          </view>
        </view>
      </view>

      <view class="order-footer">
        <view class="order-info">
          <view class="datetime">{{ order.completeTime }}</view>
          <view class="total-info">
            <view class="totalnum">共{{ order.productList.length }}件商品，实付金额：</view>
            <view class="nums">¥{{ order.orderAmount.toFixed(2) }}</view>
          </view>
        </view>

        <view class="action-button" v-if="order.storageStatusName === '未入库'">
          <view class="buttons" @click="handleStorage(order.orderId)">去入库</view>
        </view>
      </view>
    </view>
    <up-loadmore :loading-text="loadStatus" v-if="currentPage > 1" />
  </view>
</template>

<script setup>
import { shopApi } from "@/api";
import { ref } from "vue";

const orders = ref([]);
const currentPage = ref(1);
const totalCount = ref(0);
let loadStatus = ref("正在加载中");
// 获取订单列表
const getOrderList = () => {
  // 调用接口获取订单列表
  let params = {
    currentPage: currentPage.value,
    pageSize: 15
  };
  shopApi.ORDERGETORDERLIST(params).then((res) => {
    if (res.code != 200) {
      uni.showToast({
        title: res.errMsg,
        icon: "none"
      });
      return;
    }
    orders.value = res.result.data || [];
  });
};
const handleStorage = (orderId) => {
  uni.navigateTo({
    url: `/pagesshop/inventory/orderentry?id=${orderId}`
  });
};
onLoad((options) => {
  getOrderList();
});
onPullDownRefresh(() => {
  uni.showToast({
    title: "下拉刷新",
    icon: "none",
    duration: 1000
  });
  currentPage.value = 1;
  orders.value = [];
  totalCount.value = 0;
  getOrderList();
});

onReachBottom(() => {
  if (orders.value.length >= totalCount.value) {
    loadStatus.value = "没有更多数据了";
    return;
  }
  currentPage.value++;
  getOrderList();
});
</script>

<style lang="scss" scoped>
.order-list {
  padding: 20rpx;
  box-sizing: border-box;
  background-color: #f5f5f5;
  min-height: 100vh;

  .order-card {
    background-color: #ffffff;
    border-radius: 16rpx;
    padding: 24rpx;
    box-sizing: border-box;
    margin-bottom: 20rpx;

    .store-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20rpx;

      .store-header-tit {
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 400;
        line-height: 20px;
        text-align: left;
        text-underline-position: from-font;
        text-decoration-skip-ink: none;
        color: rgba(44, 44, 44, 1);
      }

      .store-header-right {
        display: flex;
        align-items: center;

        .status {
          font-family: PingFang SC;
          font-size: 12px;
          font-weight: 400;
          line-height: 20px;
          text-align: center;
          text-underline-position: from-font;
          text-decoration-skip-ink: none;
          color: rgba(214, 142, 68, 1);
        }

        .status1 {
          font-family: PingFang SC;
          font-size: 12px;
          font-weight: 400;
          line-height: 20px;
          text-align: center;
          text-underline-position: from-font;
          text-decoration-skip-ink: none;
          color: rgba(227, 115, 24, 1);
          padding: 2px 8px 2px 8px;
          box-sizing: border-box;
          gap: 8px;
          border-radius: 3px;
          background: rgba(255, 241, 233, 1);
          margin-left: 20rpx;
        }
      }
    }

    .products {
      .product-item {
        display: flex;
        margin-bottom: 20rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .product-left{
          display: flex;
        }
        .product-img {
          width: 160rpx;
        }
        .product-info {
          flex: 1;
          margin-left: 20rpx;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding: 10rpx 0;
          .name {
            font-family: PingFang SC;
            font-size: 14px;
            font-weight: 400;
            line-height: 22px;
            text-align: left;
            text-underline-position: from-font;
            text-decoration-skip-ink: none;
            color: rgba(0, 0, 0, 0.9);
          }
          .price {
            font-family: PingFang SC;
            font-size: 14px;
            font-weight: 400;
            line-height: 22px;
            text-align: left;
            text-underline-position: from-font;
            text-decoration-skip-ink: none;
            color: rgba(0, 0, 0, 1);
          }
        }
      }
    }

    .order-footer {
      border-top: 2rpx solid #f5f5f5;
      padding-top: 20rpx;

      .order-info {
        display: flex;
        gap: 10rpx;
        margin-bottom: 20rpx;
        justify-content: space-between;
        .datetime {
          font-family: PingFang SC;
          font-size: 12px;
          line-height: 20px;
          text-align: left;
          color: rgba(0, 0, 0, 0.6);
        }

        .total-info {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          font-family: PingFang SC;
          font-size: 12px;
          line-height: 22px;
          .nums {
            color: rgba(193, 122, 50, 1);
          }
        }
      }

      .action-button {
        display: flex;
        justify-content: flex-end;
        .buttons {
          width: 180rpx;
          padding: 8px 16px 8px 16px;
          box-sizing: border-box;
          gap: 4px;
          border-radius: 20px;
          background: rgba(214, 142, 68, 1);
          color: #fff;
          font-size: 22rpx;
          text-align: center;
        }
      }
    }
  }
}
</style>
